<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Parallel;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Utils\Exception\ParallelExecutionException;
use Hyperf\Database\Model\SoftDeletes;
/**
 * @property int $id 主键ID
 * @property int $pid 父级ID
 * @property string $title 菜单标题
 * @property string $icon 图标
 * @property string $path 菜单路径
 * @property string $tag 类型：0菜单 1节点
 * @property int $status 状态：1正常 2禁用
 * @property string $remark 备注
 * @property int $sort 显示顺序
 * @property int $created_user 添加人
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $deleted_at 软删除
 */
class AppMenu extends BaseModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'app_menus';
    protected $hidden = ['deleted_at'];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'pid', 'title','target','menu_tags','service_column','topic_classification','is_show', 'icon','max_icon', 'path', 'tag', 'status', 'remark', 'sort', 'created_user', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'pid' => 'integer',  'target' => 'integer','is_show' => 'integer', 'status' => 'integer', 'sort' => 'integer', 'created_user' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
    /**
     * [createuser 创建者]
     * @return [type] [description]
     */
    public function createuser()
    {
        return $this->hasOne(User::class, 'id', 'created_user');
    }
    /**
     * [getSouyeMenu 获取首页菜单]
     * @return array 返回组装后菜单
     * @Cacheable(prefix="AppMenu",ttl=9000, listener="AppMenu")
     */
    public static function getSouyeMenu():array
    {
        $data = parallel([
            function () {
                return AppMenu::where([['status','=',1],['is_show','=',1]])->orderBy('sort','desc')->orderBy('id','desc')->limit(16)->get();
            },
            function () {
                $bmfw = [];
                $bmfw[] = ['name'=>'找','menu'=>AppMenu::where([['status','=',1],['service_column','=','帮您找找']])->orderBy('sort','desc')->orderBy('id','desc')->get()];
                $bmfw[] = ['name'=>'查','menu'=>AppMenu::where([['status','=',1],['service_column','=','帮您查查']])->orderBy('sort','desc')->orderBy('id','desc')->get()];
                $bmfw[] = ['name'=>'约','menu'=>AppMenu::where([['status','=',1],['service_column','=','帮您约约']])->orderBy('sort','desc')->orderBy('id','desc')->get()];
                return $bmfw;
            },
            function () {
                return AppMenu::where([['status','=',1],['service_column','=','主题服务']])->orderBy('sort','desc')->orderBy('id','desc')->limit(9)->get();
            },
            function () {
                return AppMenu::where([['status','=',1],['service_column','=','特色专区']])->orderBy('sort','desc')->orderBy('id','desc')->limit(9)->get();
            },
            function () {
                $ztfl = [];$zhuanti = [];
                AppMenu::where([['status','=',1],['topic_classification','<>','']])->orderBy('sort','desc')->orderBy('id','desc')->get()->map(function($item)use(&$ztfl){
                    $ztfl[$item['topic_classification']][] = $item;
                });
                foreach ($ztfl as $key => $item) $zhuanti[] = ['name'=>$key,'menu'=>$item];
                return $zhuanti;
            }
        ]);
        return array_combine(['syzs','bmfw','ztfw','tszq','ztfl'],$data);
    }
}